package com.engineering.elink.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.co.recruit.JobAddCo;
import com.engineering.elink.co.recruit.JobCo;
import com.engineering.elink.co.recruit.JobSearchCo;
import com.engineering.elink.common.ErrorType;
import com.engineering.elink.common.Pagination;
import com.engineering.elink.domain.KeyWord;
import com.engineering.elink.domain.recruit.FunctionsTopCategory;
import com.engineering.elink.domain.recruit.Job;
import com.engineering.elink.domain.recruit.Language;
import com.engineering.elink.domain.recruit.LanguageGrade;
import com.engineering.elink.domain.recruit.MajorSecondCategory;
import com.engineering.elink.domain.recruit.MajorTopCategory;
import com.engineering.elink.domain.recruit.ProfessionalDetailCategory;
import com.engineering.elink.recruitdao.CompanyViewMapper;
import com.engineering.elink.recruitdao.CvDatabaseMapper;
import com.engineering.elink.recruitdao.DictCategoryMapper;
import com.engineering.elink.recruitdao.FunctionsDetailCategoryMapper;
import com.engineering.elink.recruitdao.FunctionsTopCategoryMapper;
import com.engineering.elink.recruitdao.JobMapper;
import com.engineering.elink.recruitdao.LanguageGradeMapper;
import com.engineering.elink.recruitdao.LanguageMapper;
import com.engineering.elink.recruitdao.MajorSecondCategoryMapper;
import com.engineering.elink.recruitdao.MajorTopCategoryMapper;
import com.engineering.elink.recruitdao.ProfessionalDetailCategoryMapper;
import com.engineering.elink.recruitdao.TargetCompanyMapper;
import com.engineering.elink.recruitdao.VitaDeliveryBakMapper;
import com.engineering.elink.recruitdao.VitaDeliveryMapper;
import com.engineering.elink.service.JobService;
import com.engineering.elink.util.CollectionUtil;
import com.engineering.elink.util.ResJsonUtil;
import com.engineering.elink.videodao.KeyWordMapper;
import com.engineering.elink.vo.recruit.DeliveryJobVo;
import com.engineering.elink.vo.recruit.DictCategoryVo;
import com.engineering.elink.vo.recruit.FunctionsDetailCategoryVo;
import com.engineering.elink.vo.recruit.JobVo;
import com.engineering.elink.xaservice.CvdDatabaseXaService;
import com.engineering.elink.xaservice.EvaluateXaService;


@Service
public class JobServiceImpl implements JobService{
	
	private static Logger logger = Logger.getLogger(JobServiceImpl.class);
	@Autowired
	private DictCategoryMapper dictCategoryMapper;
	@Autowired
	private FunctionsTopCategoryMapper functionsTopCategoryMapper;
	@Autowired
	private FunctionsDetailCategoryMapper functionsDetailCategoryMapper;
	@Autowired
	private LanguageMapper languageMapper;
	@Autowired
	private MajorTopCategoryMapper majorTopCategoryMapper;
	@Autowired
	private MajorSecondCategoryMapper majorSecondCategoryMapper;
	@Autowired
	private ProfessionalDetailCategoryMapper professionalDetailCategoryMapper;
	@Autowired
	private LanguageGradeMapper languageGradeMapper;
	@Autowired
	private EvaluateXaService evaluateXaService;
	@Autowired
	private JobMapper jobMapper;
	@Autowired
	private VitaDeliveryMapper vitaDeliveryMapper;
	@Autowired
	private CompanyViewMapper companyViewMapper;
	@Autowired
	private CvDatabaseMapper cvDatabaseMapper;
	@Autowired
	private TargetCompanyMapper targetCompanyMapper;
	@Autowired
	private CvdDatabaseXaService cvdDatabaseXaService;
	@Autowired
	private KeyWordMapper keyWordMapper;
	@Autowired
	private VitaDeliveryBakMapper vitaDeliveryBakMapper;
	
	@Override
	public List<DictCategoryVo> getDictCategoryByCode(String dictCode,Integer type) {
		
		List<DictCategoryVo> dcList=dictCategoryMapper.selectAllByDictCode(dictCode);
		if(type!=null){
			if(type.intValue()==1){
				getDictVoTypeList(dcList,"jobType");
			}else if(type.intValue()==2){
				getDictVoTypeList(dcList,"workyear");
			}else if(type.intValue()==3){
				getDictVoTypeList(dcList,"educationBackground");
			}else if(type.intValue()==4){
				getDictVoTypeList(dcList,"companyType");
			}else if(type.intValue()==5){
				getDictVoTypeList(dcList,"companyGuild");
			}
		}
		
		return dcList;
	}
	
	private void getDictVoTypeList(List<DictCategoryVo> dcList,String type){
		for(DictCategoryVo vo:dcList){
			if(vo==null)continue;
			vo.setType(type);
		}
	}

	@Override
	public List<FunctionsTopCategory> getFunctionsTopCategory() {
		return functionsTopCategoryMapper.selectAll();
	}

	@Override
	public List<FunctionsDetailCategoryVo> getFuncDetailCategoryByFtcId(Integer ftcId) {
		return functionsDetailCategoryMapper.selectAllByFtcId(ftcId);
	}

	@Override
	public List<Language> getLanguage() {
		return languageMapper.selectAll();
	}
	
	
	@Override
	public List<MajorTopCategory> getMajorTopCategory() {
		return this.majorTopCategoryMapper.selectAll();
	}

	@Override
	public List<MajorSecondCategory> getMajorScByMdcId(Integer mdcId) {
		return this.majorSecondCategoryMapper.selectAllByMdcId(mdcId);
	}
	
	@Override
	public List<ProfessionalDetailCategory> getProfessionalByMscId(Integer mscId) {
		return this.professionalDetailCategoryMapper.selectAllByMscId(mscId);
	}

	@Override
	public List<LanguageGrade> getLanguageGrade() {
		return languageGradeMapper.selectAll();
	}

	@Override
	public Map<String, Object> add(JobAddCo addCo, String userId) {
		logger.info("发布职位开始！！！");
		try {
			
			String jobId=evaluateXaService.saveJob(addCo, userId);
			
			return ResJsonUtil.getMap(ErrorType.SUCCESS,jobId);
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return ResJsonUtil.getMap(ErrorType.JOB_ADD_ERROR);
	}

	@Override
	public Map<String, Object> pause(String jobId) {
		
		Job job=jobMapper.selectByPrimaryKey(jobId);
		if(job==null){
			return ResJsonUtil.getMap(ErrorType.JOB_ERROR);
		}
		job.setCloseStatus(1);
		jobMapper.updateByPrimaryKeySelective(job);
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> get(String jobId, String userId) {
		JobVo jobVo=jobMapper.selectInfo(jobId, userId);
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, jobVo);
	}

	@Override
	public Map<String, Object> update(String jobId, JobAddCo addCo, String userId) {
		logger.info("修改职位开始！！！");
		try {
			
			Job job=jobMapper.selectByPrimaryKey(jobId);
			if(job==null)
				return ResJsonUtil.getMap(ErrorType.JOB_ERROR);
			
			evaluateXaService.updateJob(job,addCo, userId);
			
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
		return ResJsonUtil.getMap(ErrorType.JOB_ADD_ERROR);
	}

	@Override
	public Map<String, Object> deleteJob(String jobIds) {
		
		if(StringUtils.isNotEmpty(jobIds)){
			
			try {
				cvdDatabaseXaService.updateBatchJob(StringUtils.split(jobIds, ','));
				
				return ResJsonUtil.getMap(ErrorType.SUCCESS);
			} catch (Exception e) {
				e.printStackTrace();
				return ResJsonUtil.getMap(ErrorType.BATCH_JOB_ERROR,e.getMessage());
			}
			
		}
		
		return ResJsonUtil.getMap(ErrorType.REQUEST_PARAMS_ERROR);
	}
	
	@Override
	public Map<String, Object> refreshJob(String jobIds) {
		
		if(StringUtils.isNotEmpty(jobIds)){
			jobMapper.updateBatchByRefreshTime(StringUtils.split(jobIds, ','));
		}
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}

	@Override
	public Map<String, Object> getJobsList(JobCo co, String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
	
		map.put("list", jobMapper.selectList(co,userId));
		map.put("Pagination", new Pagination(co, jobMapper.selectCount(co,userId)));
		
		// 将关键词存入表中
		KeyWord kw = keyWordMapper.selectByKeyWord(co.getKeyWord(),2);
		
		if (kw != null) {// 表示该关键词存在
			kw.setSearchCount(kw.getSearchCount() + 1);
			keyWordMapper.updateByPrimaryKeySelective(kw);
		} else {
			if (co.getKeyWord() != null && co.getKeyWord() != "") {
				keyWordMapper.insertSelective(new KeyWord(co.getKeyWord(),1,2));
			}
		}
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> getCompanyJob(String jobId, Integer count, String userId) {
		Job job=jobMapper.selectByPrimaryKey(jobId);
		if(job==null)
			return ResJsonUtil.getMap(ErrorType.JOB_ERROR);
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, jobMapper.selectJobByCompanyId(job.getJobId(),job.getCompanyId(),count));
	}

	@Override
	public Map<String, Object> getSameJob(String jobId, Integer count, String userId) {
		Job job=jobMapper.selectByPrimaryKey(jobId);
		if(job==null)
			return ResJsonUtil.getMap(ErrorType.JOB_ERROR);
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, jobMapper.selectSameJob(job.getJobId(),job.getJobName(),count));
	}

	@Override
	public Map<String, Object> deliveryJob(String jobId, String userId) {
		
		logger.info("投递职位开始！！！");
		
		try {
			
			this.evaluateXaService.deliveryJob(jobId, userId);
			
			return ResJsonUtil.getMap(ErrorType.SUCCESS);
			
		} catch (Exception e) {
			e.printStackTrace();
			return ResJsonUtil.getMap(ErrorType.DELIVERY_ADD_ERROR,e.getMessage());
		} 
		
	}
	
	
	@Override
	public Map<String, Object> getMyDeliveryJob(String fullName,BaseCo co, String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("fullName", fullName);
		map.put("userId", userId);
		map.put("co", co);
		List<DeliveryJobVo> voList=jobMapper.selectMyDeliveryJobList(map);
		int count=jobMapper.countMyDeliveryJob(map);
		map.clear();
		map.put("list", voList);
		map.put("Pagination", new Pagination(co, count));
		
		// 将关键词存入表中
		KeyWord kw = keyWordMapper.selectByKeyWord(fullName,2);
		
		if (kw != null) {// 表示该关键词存在
			kw.setSearchCount(kw.getSearchCount() + 1);
			keyWordMapper.updateByPrimaryKeySelective(kw);
		} else {
			if (StringUtils.isNotEmpty(fullName)) {
				keyWordMapper.insertSelective(new KeyWord(fullName,1,2));
			}
		}
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, map);
	}
	
	@Override
	public Map<String, Object> targetJob(String jobIds,String userId) {
		
		if(StringUtils.isNotEmpty(jobIds)){
			String[] array=StringUtils.split(jobIds, ',');
			
			if(CollectionUtil.arrayIsNotEmpty(array)){
//				if(array.length>3)
//					return ResJsonUtil.getMap(ErrorType.TARGET_COMPANY_ERROR);
				
				try{
					cvdDatabaseXaService.saveTargetJob(array, userId);
					
					return ResJsonUtil.getMap(ErrorType.SUCCESS);
					
				}catch(Exception e){
					e.printStackTrace();
					return ResJsonUtil.getMap(ErrorType.TARGET_COMPANY_ADD_ERROR,e.getMessage()); 
					
				}
			}
			
		}
		
		return ResJsonUtil.getMap(ErrorType.REQUEST_PARAMS_ERROR);
	}

	@Override
	public Map<String, Object> getMyCollectionJob(BaseCo co, String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		map.put("co", co);
		List<DeliveryJobVo> voList=jobMapper.selectMyCollectionJobList(map);
		int count=jobMapper.countMyCollectionJob(map);
		map.clear();
		map.put("list", voList);
		map.put("Pagination", new Pagination(co, count));
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> getSearchJobList(JobSearchCo co, String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("list", jobMapper.selectSearchJobList(co));
		map.put("Pagination", new Pagination(co, jobMapper.countSearchJob(co)));
		
		// 将关键词存入表中
		KeyWord kw = keyWordMapper.selectByKeyWord(co.getKeyWord(),2);
		
		if (kw != null) {// 表示该关键词存在
			kw.setSearchCount(kw.getSearchCount() + 1);
			keyWordMapper.updateByPrimaryKeySelective(kw);
		} else {
			if (co.getKeyWord() != null && co.getKeyWord() != "") {
				keyWordMapper.insertSelective(new KeyWord(co.getKeyWord(),1,2));
			}
		}
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, map);
	}
	
	@Override
	public Map<String, Object> getKeyWord(int count) {
		//工程技能默认取为1的
		return ResJsonUtil.getMap(ErrorType.SUCCESS,keyWordMapper.getKeyWord(count,2));
	}
	
}
